package com.xxx.service;

import cn.hutool.core.util.StrUtil;
import com.xxx.property.XDemoProperties;

/**
 * the class of advance
 *
 * <p>
 * .
 *
 * @author little_lunatic
 * @date 2024-06-13
 */
public class XDemoService {

    private final XDemoProperties xDemoProperties;

    public XDemoService(XDemoProperties xDemoProperties) {
        this.xDemoProperties = xDemoProperties;
    }

    /**
     * Says hello to the specified name.
     *
     * @param name the name to greet. Should not be null or empty.
     * @return a greeting message.
     * @throws IllegalArgumentException if the provided name is null or empty.
     */
    public String sayHello(String name) throws IllegalArgumentException {
        // 检查输入参数 name 是否为 null 或空字符串
        if (name == null || name.trim().isEmpty()) {
            throw new IllegalArgumentException("Name cannot be null or empty.");
        }
        String sanitize = sanitize(name);
        return "Hello, " + (StrUtil.isBlank(xDemoProperties.getPrefix()) ? "" : xDemoProperties.getPrefix())
                + sanitize
                + (StrUtil.isBlank(xDemoProperties.getSuffix()) ? "" : xDemoProperties.getSuffix()) + "!";
    }

    /**
     * 对输入的字符串进行简单的清理和转义，以防止潜在的安全问题。
     * 注意：这只是一个示例方法，具体实现应根据实际需求定制。
     *
     * @param input 待清理的字符串
     * @return 清理后的字符串
     */
    private static String sanitize(String input) {
        // 示例：简单清理，去除可能的HTML标签
        // 实际应用中，根据需要进行更详细的验证和转义
        return input.replaceAll("<[^>]*>", "");
    }

}
